﻿/*
Copyright (c) 2010 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace TeachMe.Application.Controls
{
    public class CaptionGroup : Panel
    {
        private const int TextIndent = 10;
        private const int StripeIndent = 4;
        private const int StripeHeight = 20;

        public CaptionGroup()
        {
            SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
        }

        [Localizable(true)]
        public string Caption { get; set; }

        protected override void OnResize(System.EventArgs eventargs)
        {
            Invalidate();
            
            base.OnResize(eventargs);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            SizeF textSize = e.Graphics.MeasureString(Caption, Font);
            Rectangle rectangle = new Rectangle(StripeIndent, StripeIndent, Width - StripeIndent * 2, StripeHeight);
            Brush brush = new LinearGradientBrush(rectangle, Color.LightSlateGray, Color.WhiteSmoke, LinearGradientMode.Vertical);
            
            e.Graphics.FillRectangle(brush, StripeIndent, StripeIndent, Width - StripeIndent * 2, StripeHeight);
            e.Graphics.DrawString(Caption, Font, Brushes.Black, TextIndent, (StripeIndent + StripeHeight / 2) - ((int)textSize.Height / 2));

            base.OnPaint(e);
        }
    }
}